from rest_framework.viewsets import ModelViewSet
from rest_framework.response import Response
from rest_framework.views import APIView
from django.http import HttpResponse
from .sers import *
import jwt

from captcha.image import ImageCaptcha  # 生成图片
from io import BytesIO  # 字节流
import redis  # redis数据库
import random  # 随机模块
import re  # 正则

r = redis.Redis(host='localhost', port='6379', decode_responses=True)  # 连接redis的


class RegisterView(APIView):
    def post(self, request):
        username = request.data.get('username', None)
        password = request.data.get('password', None)
        phone = request.data.get('phone')
        code=request.data.get('code')
        rcode=r.get(phone)
        if code==rcode:
            if not all({username, password, phone}):
                return Response({'msg': '参数不完整', 'code': 400})
            user_obj = User.objects.filter(username=username).first()
            # 判断用户是否存在
            if user_obj:
                return Response({'msg': '用户已存在', 'code': 400})
            else:
                if not re.match(r'^1[3-9]\d{9}$', phone):
                    return Response({'msg': '号码不合法', 'code': 400})
                else:
                    User.objects.create(username=username, password=password, phone=phone)
                    return Response({'msg': '注册成功', 'code': 200})
        else:
            return Response({'msg':'手机验证码输入错误','code':400})


# 生成图片验证
class TupianView(APIView):
    def get(self, request):
        code = CodeUtils.make_code_n(4)
        uu_id = request.GET.get('uu_id')
        # for i in range(0, 4):
        #     code += str(random.randint(0, 9))
        # print(code)
        a1 = ImageCaptcha().generate_image(code)
        zj = BytesIO()
        a1.save(zj, 'jpeg')
        r.set(str(uu_id), code, ex=30)
        return HttpResponse(zj.getvalue(), content_type='image/jpeg')


import uuid


class YZM(APIView):
    def get(self, request):
        uu_id = uuid.uuid4()
        return Response({'uu_id': uu_id})

# uuid和前端的验证码对比
class PD(APIView):
    def get(self, request):
        yzm = request.GET.get('yzm')
        uu_id = request.GET.get('uu_id')
        yzm1 = r.get(str(uu_id))
        if not yzm1:
            return Response({'msg': '验证码已过期,请点击刷新验证码', 'code': 300})
        if yzm == yzm1:
            return Response({'msg': '验证码正确', 'code': 200})
        else:
            return Response({'msg': '验证码不正确', 'code': 300})

# 手机验证码
from ronglian_sms_sdk import SmsSDK
from Workorder import settings
from .duanxin import CodeUtils
class send_message(APIView):
    def post(self,request):
        phone=request.data.get('phone')
        code=CodeUtils.make_code_n(4)
        r.set(phone,code,60)
        sdk = SmsSDK(settings.accid,settings.acctoken,settings.appid)
        sdk.sendMessage(1,phone,(code,2))
        return Response({'msg':'短信发送成功,请查收','code':200})





# 登录
class LongView(APIView):
    def post(self,request):
        username=request.data.get('username')
        password=request.data.get('password')
        user=User.objects.filter(username=username,password=password).first()
        if user:
            token=jwt.encode({'uid':user.id},'123')
            return Response({'msg':'登录成功','code':200,'token':token})
        else:
            return Response({'msg':'登录失败','code':400})



































